//package com.sky.service.impl;
//
//import cn.hutool.core.util.IdcardUtil;
//import com.sky.constant.PasswordConstant;
//import com.sky.entity.SysPermission;
//import com.sky.entity.SysRole;
//import com.sky.entity.SysUser;
//import com.sky.mapper.*;
//import com.sky.result.Result;
//import com.sky.service.SysUserService;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.core.userdetails.User;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UsernameNotFoundException;
//import org.springframework.security.crypto.password.PasswordEncoder;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.time.LocalDateTime;
//import java.util.*;
//
//@Service
//public class SysUserServiceImpl implements SysUserService {
//    @Autowired
//    private SysUserMapper sysUserMapper;
//    @Autowired
//    private PasswordEncoder passwordEncoder;
//
//    @Override
//    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        SysUser user= sysUserMapper.getByUsername(username);
//        if (user==null) {
//            throw new UsernameNotFoundException("账户不存在！");
//        }
//        List<SysRole> lis=new ArrayList<>();
//        SysRole sysRole = new SysRole();
//        sysRole.setStatus(1);
//        sysRole.setId(3);
//        sysRole.setName("普通用户");
//
//        user.setAuthorities(lis);
////        Set<SysPermission> permissions=new HashSet<>();
////        SysPermission sysPermission = new SysPermission();
////        sysPermission.setId(1);
////        sysPermission.setPermName("p1");
////        sysPermission.setParentId(0);
////        sysPermission.setPermUrl("1");
////        permissions.add(sysPermission);
////        user.setPermissionSet(permissions);
////        return new User(
////                user.getUsername(),
////                user.getPassword(),// 数据库中的密码应为哈希值
////                user.getAuthorities()
////        );
//        return user;
//    }
//
//    @Override
//    @Transactional
//    public Result register(SysUser sysUser) {
//        SysUser username = sysUserMapper.getByUsername(sysUser.getUsername());
//        if(username!=null) {
//            return Result.error("用户已存在");
//        }
//        if(sysUser.getIdCard()!=null&&StringUtils.isNotBlank(sysUser.getIdCard())){
//            if(!IdcardUtil.isValidCard(sysUser.getIdCard())){
//                return Result.error("身份证不合法");
//            }
//        }
//        sysUser.setPassword(StringUtils.isBlank(sysUser.getPassword()) ?
//                passwordEncoder.encode(PasswordConstant.DEFAULT_PASSWORD) :
//                passwordEncoder.encode(sysUser.getPassword()));
//        sysUser.setCreateTime(LocalDateTime.now());
//        sysUser.setUpdateTime(LocalDateTime.now());
//        sysUser.setDelFlag(false);
//        sysUser.setStatus(1);
//        sysUserMapper.add(sysUser);
//        return Result.success();
//    }
//
//    @Override
//    public SysUser loginByPhone(String phone) {
//        SysUser user= sysUserMapper.getByMobile(phone);
//        //存在该用直接返回用户对
//        if(user!=null) {
//            return user;
//        }
//        //不存在该用户则注册该用户
//        SysUser sysUser = new SysUser();
//        sysUser.setUsername(phone);
//        sysUser.setUserid(phone);
//        sysUser.setPassword(passwordEncoder.encode(PasswordConstant.DEFAULT_PASSWORD));
//        sysUser.setPassword(passwordEncoder.encode(PasswordConstant.DEFAULT_PASSWORD));
//        sysUser.setDelFlag(false);
//        sysUser.setCreateTime(LocalDateTime.now());
//        sysUser.setUpdateTime(LocalDateTime.now());
//        sysUser.setCreateBy(0);
//        sysUser.setUpdateBy(0);
//        sysUser.setMobile(phone);
//        sysUser.setStatus(1);
//        sysUserMapper.add(sysUser);
//        return sysUser;
//    }
//
//    @Override
//    public SysUser getById(Integer id) {
//        return sysUserMapper.getById(id);
//    }
//}
